home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BMP2BPI.ZIP / bmp2bpi.asm next >
Assembly Source File  |  1994-12-17  |  12KB  |  751 lines

  1. page 60,132
  2.  
  3. ; BMP to BPI (Bit Plane Image) converter
  4. ;
  5. ;
  6. ; Dave Springer, Feb. 1994
  7. ; Copyright (C) 1994 Springer Engineering
  8. ;
  9. ; misc equates
  10. ;
  11. cr    equ    0dh
  12. lf    equ    0ah
  13. eom    equ    '$'
  14.  
  15. cseg     segment   para public 'CODE'
  16. ;
  17.      org       100h    
  18.  
  19.          assume    cs:cseg,ds:cseg,es:cseg,ss:cseg
  20.  
  21. plane1 equ 2        ;2
  22. plane2 equ 3        ;3
  23. plane3 equ 4        ;1
  24. plane4 equ 1        ;4
  25.  
  26. transparent_plane equ 5
  27.  
  28. post   proc      near          
  29.  
  30.     call get_filenames
  31.     call initialize            ;initialize file stuff
  32.  
  33. start_plane equ 2
  34.  
  35.     mov plane_number,plane1
  36.     call state_start
  37.     call store_header
  38.     call do_plane
  39.  
  40.     mov plane_number,plane2
  41.     call state_start
  42.     call store_header
  43.     call do_plane
  44.  
  45.     mov plane_number,plane3
  46.     call state_start
  47.     call store_header
  48.     call do_plane
  49.  
  50.     mov plane_number,plane4
  51.     call state_start
  52.     call store_header
  53.     call do_plane
  54.  
  55.     cmp transparent_plane_flag,1
  56.     jne no_transparent_plane
  57.     mov plane_number,transparent_plane
  58.     call state_start
  59.     call store_header
  60.     call do_plane
  61. no_transparent_plane:
  62.     jmp end_program            ;finish it off
  63.  
  64. ;***************** subroutines (start here) *****************
  65. ; hahhahaha. Verbose, aren't I ?
  66. ;**********************************
  67.  
  68. store_header:
  69.     cmp plane_number,plane1
  70.     jne store_header_done
  71.  
  72.     mov bx,rows
  73.     mov al,bl
  74.     call store_sample
  75.     mov al,bh
  76.     call store_sample
  77.     mov bx,columns
  78.     mov al,bl
  79.     call store_sample
  80.     mov al,bh
  81.     call store_sample
  82.     mov al,transparent_plane_flag
  83.     call store_sample
  84.  
  85.     mov cx,11
  86. sth_loop:
  87.     xor al,al
  88.     call store_sample
  89.     loop sth_loop
  90.  
  91. store_header_done:
  92.     mov ax,columns
  93.     ror ax,1
  94.     ror ax,1
  95.     ror ax,1
  96.     test ah,0e0h
  97.     jz ss_1
  98.     and ah,1fh
  99.     inc ax
  100. ss_1:
  101.     shl ax,1
  102.     shl ax,1
  103.     mov bytes_per_row,ax
  104.  
  105.     mov ah,42h
  106.     mov al,2
  107.     mov bx,in_handle
  108.     xor cx,cx
  109.     dec cx
  110.     xor dx,dx
  111.     sub dx,bytes_per_row
  112.     int 21h
  113.     mov ah,3fh
  114.     mov bx,in_handle
  115.     mov cx,bytes_per_row
  116.     mov dx,offset in_buf
  117.     int 21h
  118.     ret
  119.  
  120. do_plane:
  121.     mov cx,bytes_per_row
  122.     shr cx,1
  123.     shr cx,1
  124.     mov si,offset in_buf
  125. do_row:
  126.     push cx
  127.     cmp plane_number,transparent_plane
  128.     jne do_normal_row
  129.     call make_transparent_byte
  130.     pop cx
  131.     loop do_row
  132.     jmp dp_row_done
  133. do_normal_row:
  134.     call make_byte
  135.     pop cx
  136.     loop do_row
  137. dp_row_done:
  138.     dec rows
  139.     jz do_plane_done
  140.     call back_up_2_rows
  141.     jmp do_plane
  142. do_plane_done:
  143.     ret
  144.  
  145. make_byte:
  146.     mov ch,plane_number
  147.     xor bl,bl
  148.     lodsb
  149.  
  150.     mov cl,ch
  151.     rol al,cl
  152.     rcl bl,1
  153.     mov cl,4
  154.     rol al,cl
  155.     rcl bl,1
  156.  
  157.     lodsb
  158.  
  159.     mov cl,ch
  160.     rol al,cl
  161.     rcl bl,1
  162.     mov cl,4
  163.     rol al,cl
  164.     rcl bl,1
  165.  
  166.     lodsb
  167.  
  168.     mov cl,ch
  169.     rol al,cl
  170.     rcl bl,1
  171.     mov cl,4
  172.     rol al,cl
  173.     rcl bl,1
  174.  
  175.     lodsb
  176.  
  177.     mov cl,ch
  178.     rol al,cl
  179.     rcl bl,1
  180.     mov cl,4
  181.     rol al,cl
  182.     rcl bl,1
  183.  
  184.     mov al,bl
  185.     call store_sample
  186.     ret
  187.  
  188.  
  189. make_transparent_byte:
  190.  
  191.     xor bl,bl
  192.     mov cx,4
  193. mtb_src_byte_loop:
  194.     push cx
  195.     mov cl,4
  196.     lodsb
  197.     mov ah,al
  198.     rol al,cl
  199.     and al,0fh
  200.     cmp al,transparent_plane_color
  201.     clc
  202.     jz mtb_even_pix_done
  203.     stc
  204. mtb_even_pix_done:
  205.     rcl bl,1
  206.     and ah,0fh
  207.     cmp ah,transparent_plane_color
  208.     clc
  209.     jz mtb_odd_pix_done
  210.     stc
  211. mtb_odd_pix_done:
  212.     rcl bl,1
  213.     pop cx
  214.     loop mtb_src_byte_loop
  215.  
  216.     mov al,bl
  217.     call store_sample
  218.     ret
  219.  
  220. plane_number db start_plane
  221. rows dw 0
  222. columns dw 0
  223. data_start dw 0
  224. bytes_per_row dw 0
  225. transparent_plane_flag db 0
  226. transparent_plane_color db 0
  227.  
  228. back_up_2_rows:
  229.     push ax
  230.     push bx
  231.     push cx
  232.     push dx
  233.     mov ah,42h
  234.     mov al,1
  235.     mov bx,in_handle
  236.     xor cx,cx
  237.     dec cx
  238.     xor dx,dx
  239.     sub dx,bytes_per_row
  240.     sub dx,bytes_per_row
  241.     int 21h
  242.  
  243.     mov ah,3fh
  244.     mov bx,in_handle
  245.     mov cx,bytes_per_row
  246.     mov dx,offset in_buf
  247.     int 21h
  248.     pop dx
  249.     pop cx
  250.     pop bx
  251.     pop ax
  252.     ret
  253.  
  254. state_start:        
  255.     call reset_input_file    
  256.     mov cx,0ah
  257. ss_loop1:
  258.     call get_sample
  259.     loop ss_loop1
  260.  
  261.     call get_sample
  262.     mov bl,al
  263.     call get_sample
  264.     mov bh,al
  265.     mov data_start,bx
  266.  
  267.     mov cx,6
  268. ss_loop2:
  269.     call get_sample
  270.     loop ss_loop2
  271.  
  272.     call get_sample
  273.     mov bl,al
  274.     call get_sample
  275.     mov bh,al
  276.     mov columns,bx
  277.     call get_sample
  278.     call get_sample
  279.     call get_sample
  280.     mov bl,al
  281.     call get_sample
  282.     mov bh,al
  283.     mov rows,bx
  284.  
  285.     call reset_input_file
  286.  
  287.     mov cx,data_start
  288. ss_loop3:
  289.     call get_sample
  290.     loop ss_loop3
  291.  
  292.     ret
  293.  
  294. reset_input_file:
  295.     xor ax,ax
  296.     mov end_file_flag,al        ;end of file false
  297.     mov final_in_buf_top,ax        ;final inbuf false
  298.     mov in_buf_ptr,offset in_buf_top;force buffer read
  299.     mov ax,4200h            ;move file pointer to
  300. beginning of file
  301.     mov bx,in_handle        ;for the input file
  302.     xor cx,cx            ;zero into big count
  303.     mov dx,00h            ;skip over offset
  304.     int 21h                ;do it
  305.     ret
  306.  
  307. reset_output_file:
  308.     mov ah,40h            ;first flush any buffers
  309.     mov cx,out_buf_ptr
  310.     sub cx,offset out_buf
  311.     jz rof_flushed
  312.     mov dx,offset out_buf
  313.     mov bx,out_handle
  314.     int 21h                ;flush final out buffer
  315. rof_flushed:
  316.     mov out_buf_ptr,offset out_buf_top ;force buffer read
  317.     mov ax,4200h            ;move file pointer to
  318. beginning of file
  319.     mov bx,out_handle        ;for the input file
  320.     xor cx,cx            ;zero into big count
  321.     mov dx,00h            ;skip over offset
  322.     int 21h                ;do it
  323.     ret
  324.  
  325. get_filenames:
  326.     mov si,80h
  327.     mov cl,[si]
  328.     xor ch,ch        ;get command tail length
  329.     cmp cx,2        ;see if any there
  330.     jl exit1        ;else exit
  331.     mov bx,offset in_file
  332.     inc si
  333.     cmp byte ptr [si],' '
  334.     je file_parse_loop
  335. exit1:
  336.     call signon
  337.     mov dx,offset file_error_mess
  338.     mov ah,9
  339.     int 21h
  340.     int 20h
  341.  
  342. file_parse_loop:        ;parse out one filename
  343.     inc si
  344.     mov al,[si]
  345.     cmp al,cr
  346.     je finish_filename
  347.     cmp al,' '
  348.     jne filename_cont
  349.                 ;get the bit plane flag here if
  350. present
  351.     inc si
  352.     mov al,[si]
  353.     sub al,30h
  354.     jge bpf_1
  355.     jmp init_exit
  356. bpf_1:
  357.     cmp al,9
  358.     jle bpf_good
  359.     sub al,7
  360.     cmp al,0ah
  361.     jge bpf_2
  362.     jmp init_exit    
  363. bpf_2:
  364.     cmp al,0fh
  365.     jle bpf_good
  366.     jmp init_exit
  367. bpf_good:
  368.     mov transparent_plane_flag,1
  369.     mov transparent_plane_color,al
  370.     jmp finish_filename
  371. filename_cont:
  372.     mov [bx],al
  373.     inc bx
  374.     loop file_parse_loop
  375. finish_filename:
  376.     mov byte ptr [bx],'.'
  377.     inc bx
  378.     mov byte ptr [bx],'b'
  379.     inc bx
  380.     mov byte ptr [bx],'m'
  381.     inc bx
  382.     mov byte ptr [bx],'p'
  383.     inc bx
  384.     mov byte ptr [bx],0
  385.     mov si,offset in_file
  386.     mov bx,offset out_file
  387. ffn_loop:
  388.     mov al,[si]
  389.     cmp al,'.'
  390.     jne ffnl_1
  391. ;    mov byte ptr [bx],'s'
  392. ;    inc bx
  393. ffnl_1:
  394.     mov [bx],al
  395.     inc si
  396.     inc bx
  397.     cmp al,'.'
  398.     jne ffn_loop
  399.     mov byte ptr [bx],'b'
  400.     inc bx
  401.     mov byte ptr [bx],'p'
  402.     inc bx
  403.     mov byte ptr [bx],'i'
  404.     inc bx
  405.     mov byte ptr [bx],0
  406.     ret
  407.  
  408.  
  409. store_sample:            ;store sample in al and flush out
  410. buffer
  411.     push ax            ;if necessary
  412.     push bx
  413.     push cx
  414.     push dx
  415.     push di
  416.     mov di,out_buf_ptr
  417.     mov [di],al
  418.     inc di
  419.     mov out_buf_ptr,di
  420.     cmp di,offset out_buf_top
  421.     jne store_sample_done
  422.     mov ah,40h
  423.     mov cx,1000h
  424.     mov dx,offset out_buf
  425.     mov bx,out_handle
  426.     int 21h            ;flush output buffer
  427.     mov out_buf_ptr,offset out_buf
  428. store_sample_done:
  429.     pop di
  430.     pop dx
  431.     pop cx
  432.     pop bx
  433.     pop ax
  434.     ret
  435.  
  436. get_sample:        ;return next sample in al - uses AX
  437.     push bx        ;carry set if last sample
  438.     push cx
  439.     push dx
  440.     push si
  441.     mov si,in_buf_ptr
  442.     cmp end_file_flag,0
  443.     je gs_not_end_of_file
  444.     mov al,[si]
  445.     inc si
  446.     cmp si,final_in_buf_top
  447.     mov in_buf_ptr,si
  448.     jne gs_not_last_exit
  449.     stc
  450.     jmp get_sample_exit
  451. gs_not_end_of_file:
  452.     cmp si,offset in_buf_top
  453.     jne gs_no_new_buffer
  454.     mov ah,3fh
  455.     mov cx,1000h
  456.     mov bx,in_handle
  457.     mov dx,offset in_buf
  458.     int 21h            ;get first part of input file
  459.     mov si,offset in_buf
  460.     cmp ax,cx
  461.     je gs_no_new_buffer
  462.     mov end_file_flag,1    ;mark end of file if so
  463.     add ax,offset in_buf
  464.     mov final_in_buf_top,ax
  465. gs_no_new_buffer:
  466.     mov al,[si]
  467.     inc si
  468.     mov in_buf_ptr,si
  469. gs_not_last_exit:
  470.     clc
  471. get_sample_exit:
  472.     pop si
  473.     pop dx
  474.     pop cx
  475.     pop bx
  476.     ret
  477.  
  478. in_file db 60 dup (0)
  479. out_file db 60 dup (0)
  480. in_handle dw 0
  481. out_handle dw 0
  482. end_file_flag db 0
  483. final_in_buf_top dw 0
  484. in_buf_ptr dw offset in_buf
  485. out_buf_ptr dw offset out_buf
  486.  
  487. end_program:
  488.     mov ah,40h
  489.     mov cx,out_buf_ptr
  490.     sub cx,offset out_buf
  491.     jz end_program1
  492.     mov dx,offset out_buf
  493.     mov bx,out_handle
  494.     int 21h            ;flush final out buffer
  495. end_program1:
  496.     mov ah,3eh
  497.     mov bx,out_handle    
  498.     int 21h            ;close out file
  499.     mov ah,3eh
  500.     mov bx,in_handle
  501.     int 21h            ;close in file
  502.     mov dx,offset done_message
  503.     mov ah,9
  504.     int 21h            ;print done message
  505.     int 20h            ;back to dos
  506.     
  507. done_message db cr,lf,'BMP to BPI File successfully converted',eom
  508.  
  509. initialize:
  510.     mov ah,3dh
  511.     xor al,al
  512.     mov dx,offset in_file
  513.     int 21h            ;open input file for read only access
  514.     jc init_exit
  515.     mov in_handle,ax
  516.     mov ah,3ch
  517.     xor cx,cx
  518.     mov dx,offset out_file
  519.     int 21h            ;create or open and truncate output
  520. file
  521.     jc init_exit
  522.     mov out_handle,ax
  523.     mov ah,3fh
  524.     mov cx,1000h
  525.     mov bx,in_handle
  526.     mov dx,offset in_buf
  527.     int 21h            ;get first part of input file
  528.     cmp ax,cx
  529.     je init_1
  530.     mov end_file_flag,1    ;mark end of file if so
  531.     add ax,offset in_buf
  532.     mov final_in_buf_top,ax
  533. init_1:
  534.     mov si,in_buf_ptr
  535.     mov di,out_buf_ptr      ;set up buffer indexes
  536.     mov in_buf_ptr,si
  537.     mov out_buf_ptr,di    ;update buffer indexes
  538.     ret
  539. init_exit:
  540.     call signon
  541.     mov dx,offset file_error_mess
  542.     mov ah,9
  543.     int 21h
  544.     int 20h
  545.  
  546. file_error_mess db cr,lf,lf,'File Error - program aborted.',cr,lf,eom
  547.  
  548. signon:
  549.     mov dx,offset usage_mess
  550.     mov ah,9
  551.     int 21h
  552.     ret
  553.  
  554. usage_mess label byte
  555. db cr,lf
  556. db '.BMP to .BPI File Convertor V1.0 - Copyright 1993 Springer
  557. Engineering'
  558. db cr,lf,lf
  559. db '                    Usage - BMP2BPI filename [C]'
  560. db cr,lf,lf
  561. db '                    Where C is the optional color (0-9 or A-F)
  562. build a'
  563. db cr,lf
  564. db '                    transparency plane from.',cr,lf,lf
  565. db '                    (Input File Extension of .BMP is
  566. assumed)',cr,lf
  567. db '                    (Output File is [filename].BPI)',cr,lf,eom
  568.  
  569. get_key:
  570.     mov ah,1    ;get any key
  571.     int 16h
  572.     jz gks_nokey
  573.     mov ah,0    ;clear out the key and exit
  574.     int 16h
  575.     stc
  576.     ret
  577. gks_nokey:
  578.     clc
  579.     ret
  580.  
  581. text_color db 0ffh
  582. row db 0
  583. column db 0
  584.  
  585. gotoxy:
  586.     mov ah,2
  587.     xor bh,bh
  588.     mov dh,row
  589.     mov dl,column
  590.     int 10h
  591.     ret
  592.  
  593. print_string:
  594.     mov al,[si]
  595.     or al,al
  596.     je pstring_done
  597.     mov ah,14
  598.     mov bl,text_color
  599.     int 10h
  600.     inc si
  601.     jmp print_string
  602. pstring_done:
  603.     ret
  604.  
  605. print_ax:            ;PRINT THE WORD IN AX
  606.     push ax            ;AT SCN_PTR
  607.     push bx
  608.     push cx
  609.     push dx
  610.  
  611.     push si
  612.     push di
  613.     push ds
  614.     push es
  615.     push bp
  616.     pushf
  617.  
  618.     push ax
  619.     mov al,ah
  620.     xor ah,ah
  621.     call hex2ascii
  622.     push dx
  623.     mov al,dl
  624.     call print_AL
  625.     pop dx
  626.     mov al,dh
  627.     call print_al
  628.     pop ax
  629.     xor ah,ah
  630.     call hex2ascii
  631.     push dx
  632.     mov al,dl
  633.     call print_al
  634.     pop dx
  635.     mov al,dh
  636.     call print_al
  637.     mov al,20h
  638.     call print_al
  639.  
  640. print_ax_done:
  641.     popf
  642.     pop bp
  643.     pop es
  644.     pop ds
  645.     pop di
  646.     pop si
  647.     pop dx
  648.     pop cx
  649.     pop bx
  650.     pop ax
  651.     ret
  652.  
  653. print_al:
  654.     mov ah,14
  655.     mov bl,text_color
  656.     int 10h
  657.     ret
  658.  
  659. hex2ascii:    ;convert al reg to ascii in dx (low nibble in dh)
  660.     mov si,offset hex_conv
  661.     mov cx,ax
  662.     and ax,0f0h
  663.     shr ax,1
  664.     shr ax,1
  665.     shr ax,1
  666.     shr ax,1
  667.     mov bx,ax
  668.     mov dl,cs:[si+bx]
  669.     and cx,0fh
  670.     mov bx,cx
  671.     mov dh,cs:[si+bx]
  672.     ret
  673.  
  674. hex_conv     db     '0123456789abcdef'
  675.  
  676. ;*************************************** END HEX DISPLAY ROUTINES
  677. ;
  678. ; Display decimal number in BX.
  679. ; ASCII ZERO STRING RETURNED IN DECIMAL_STRING
  680. ;
  681. decimal_string db 10 dup (0)
  682.  
  683. DMSDEC:
  684.     push es
  685.     push ds
  686.     pop es
  687.     mov di,offset decimal_string
  688.     PUSH    AX
  689.     PUSH    CX
  690.     PUSH    DX
  691.     PUSH    BX        ; save regs
  692.     MOV    AL,' '        ; load space fill char
  693.     STOSB            ; pad decimal with space
  694.     XOR    AH,AH        ; clear non-display counter
  695.     XOR    CL,CL        ; clear leading zero flag
  696.     MOV    DX,10000
  697.     CALL    _DO1        ; display 10,000's digit
  698.     MOV    DX,1000
  699.     CALL    _DO1        ; display 1,000's digit
  700.     MOV    DX,100
  701.     CALL    _DO1        ; display 100's digit
  702.     MOV    DX,10
  703.     CALL    _DO1        ; display 10's digit
  704.     INC    CL
  705.     MOV    DX,1
  706.     CALL    _DO1        ; display 1's digit
  707.     OR    AH,AH        ; non-display present?
  708.     JZ    _DOX        ; if not, exit
  709.     MOV    AL,' '        ; else, load space fill
  710. _SPL:    STOSB            ; write a space
  711.     DEC    AH        ; more to write?
  712.     JNZ    _SPL        ; if so, continue
  713. _DOX:
  714.     mov al,0
  715.     stosb
  716.     pop es
  717.     POP    BX
  718.     POP    DX
  719.     POP    CX
  720.     POP    AX        ; restore regs
  721.     RET            ; done
  722. _DO1:    MOV    CH,-1        ; load quotient reg
  723. _DOL:    INC    CH        ; inc reg
  724.     SUB    BX,DX        ; try the subtract
  725.     JNB    _DOL        ; if good, continue
  726.     ADD    BX,DX        ; else, restore remainder
  727.     MOV    AL,CH        ; get quotient
  728.     OR    AL,AL        ; results=0?
  729.     JNZ    _DO2        ; if not, continue
  730.     OR    CL,CL        ; leading zero?
  731.     JNZ    _DO2        ; if not, continue
  732.     INC    AH        ; inc non-display count
  733.     RET            ; done
  734. _DO2:    INC    CL        ; inc leading zero flag
  735.     ADD    AL,"0"        ; add ASCII bias
  736.     STOSB            ; write to screen
  737.     RET            ; done
  738.  
  739.  
  740. in_buf  equ $
  741. in_buf_top equ $+1000h
  742. out_buf equ $+1000h
  743. out_buf_top equ $+2000h
  744.  
  745. post  endp
  746.             
  747. cseg    ends
  748.  
  749.          end       post
  750.  
  751.